package com.dzm.controller.admin;

import com.dzm.constants.ClientConstant;
import com.dzm.constants.RedisConstant;
import com.dzm.entity.User;
import com.dzm.service.TokenService;
import com.dzm.service.UserService;
import com.dzm.to.LoginUser;
import com.dzm.vo.LoginVo;
import com.dzm.except.Assert;
import com.dzm.utils.R;
import com.dzm.utils.ResponseEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 作者：Dzm
 * 时间：2024-08-31-12:33
 * 功能：
 */

@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;

    @PostMapping("/login")
    public R login(@RequestBody LoginVo vo) {
        Assert.notNull(vo, ResponseEnum.LOGIN_EXCEPTION);
        LoginUser loginUser = userService.login(vo);
        Map<String, Object> map = tokenService.getToken(loginUser);

        return R.ok().data(map);
    }

    @GetMapping("/info")
    public R info(@RequestHeader(ClientConstant.CLIENT_TOKEN_NAME) String token) {
        Map<String, Object> info = tokenService.getUserInfo(token);

        return R.ok().data(info);
    }
}
